using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Forms;
using System.Windows.Markup;
using System.Windows.Media;
using Common;
using Common.Enum;
using CustomChart;
using Images;
using Infragistics.UltraChart.Resources.Appearance;
using SExperiment;
using SExperiment.ExperimentResult;
using SProject.BasicInformation;
using SProject.Paramter;
using SSettings;

namespace MainView.Windows
{
	public partial class CurveParameterWindow : Window, IComponentConnector
	{
		public class CurveParameterEx
		{
			private string _WellName;

			private string _TargetName;

			public string WellName
			{
				get
				{
					return _WellName;
				}
				set
				{
					_WellName = value;
				}
			}

			public string TargetName
			{
				get
				{
					return _TargetName;
				}
				set
				{
					_TargetName = value;
				}
			}

			public Subset.CurveParameter CurveParameter { get; set; }

			public SampleTargetItem Item { get; set; }
		}

		private NormalChart _Chart;

		private Experiment _Experiment;

		private List<CurveParameterEx> list = new List<CurveParameterEx>();

		private List<Subset.CurveParameter> curveParameters = new List<Subset.CurveParameter>();

		public int MaxCycle { get; set; }

		public CurveParameterWindow(Experiment experiment, List<Well> selectedWells, List<int> selectedChannels)
		{
			_Experiment = experiment;
			MaxCycle = _Experiment.Program.GetAcquisitionCycleCount();
			InitializeComponent();
			_Chart = new NormalChart();
			_Chart.Chart.Dock = DockStyle.Fill;
			_Chart.SetTitleX(Utility.GetMessage("cycles_num"));
			_Chart.SetTitleY(Utility.GetMessage("Rn"));
			_Chart.SetXFormat("0");
			_Chart.SetYFormat("0.000");
			windowsFormsHost.Child = _Chart.Chart;
			foreach (Well selectedWell in selectedWells)
			{
				if (selectedWell.Project == null)
				{
					continue;
				}
				foreach (SampleTargetItem item in selectedWell.Targets)
				{
					if (selectedWell.MultiTubeID != item.TubeNo || !selectedChannels.Contains(item.ChannelNo))
					{
						continue;
					}
					CurveParameterEx curveParameterEx = new CurveParameterEx
					{
						WellName = selectedWell.CellName,
						TargetName = item.TargetName,
						Item = item
					};
					string key = selectedWell.ID.ToString("00") + item.ChannelNo;
					if (_Experiment.CurrentSubset.CurveParameters.ContainsKey(key))
					{
						curveParameterEx.CurveParameter = _Experiment.CurrentSubset.CurveParameters[key] as Subset.CurveParameter;
						curveParameters.Add(curveParameterEx.CurveParameter.Clone());
					}
					else
					{
						curveParameterEx.CurveParameter = new Subset.CurveParameter
						{
							WellNo = selectedWell.ID,
							ChannelNo = item.ChannelNo
						};
						BasicOption basicOption = _Experiment.CurrentSubset.GetSubsetParamter(selectedWell.Project).BasicOption;
						BasicOptionItem basicOptionItem = basicOption.Items.Where((BasicOptionItem s) => s.TubeNo == item.TubeNo && s.ChannelNo == item.ChannelNo).FirstOrDefault();
						curveParameterEx.CurveParameter.BeginBaseline = basicOptionItem.BeginBaseline;
						curveParameterEx.CurveParameter.EndBaseline = basicOptionItem.EndBaseline;
						curveParameterEx.CurveParameter.Threshold = basicOptionItem.Threshold;
						curveParameterEx.CurveParameter.Use = false;
						_Experiment.CurrentSubset.CurveParameters.Add(key, curveParameterEx.CurveParameter);
					}
					list.Add(curveParameterEx);
				}
			}
			grid.ItemsSource = list;
			RefreshChartAxisX();
			RefreshChartAxisY();
		}

		private void RefreshSeries()
		{
			_Chart.ClearAllDataSeries();
			_Chart.ClearAllThresholdSeries();
			CurveParameterEx curveParameterEx = grid.SelectedItem as CurveParameterEx;
			XYSeriesEx xYSeriesEx = new XYSeriesEx();
			xYSeriesEx.PEs.Add(new PaintElement());
			xYSeriesEx.PEs[0].Fill = Utility.WindowsColorToDrawingColor(GetChartDataColor(_Experiment.Wells[curveParameterEx.CurveParameter.WellNo], curveParameterEx.Item));
			foreach (Dot item in curveParameterEx.Item.Result.AMPCurve.CurvePoint)
			{
				xYSeriesEx.Points.Add(new XYDataPoint(item.X, item.Y, "", false));
			}
			_Chart.AddDataSeries(xYSeriesEx, GetChartDataLinePointStyle(curveParameterEx.Item));
		}

		public Color GetChartDataColor(Well well, SampleTargetItem item)
		{
			if (item.HasCustomColor(_Experiment.ColorMode))
			{
				return item.GetCustomColor(_Experiment.ColorMode);
			}
			switch (_Experiment.ColorMode)
			{
			case EColorMode.Channel:
				return ConfigSettings.GetInstance().ExperimentSetting.ColorSettings.GetChannelColor(item.ChannelNo);
			case EColorMode.Type:
				return Utility.GetSampleColor(item.SampleType);
			case EColorMode.Row:
				return Utility.GetRowColor(well.RowIndex + 1);
			case EColorMode.Column:
				return Utility.GetColumnColor(well.ColumnIndex + 1);
			case EColorMode.Result:
				if (item.Result is HRMTargetResult)
				{
					return Utility.GetDrawingGenoTypeColor(((HRMTargetResult)item.Result).GenoType);
				}
				if (well.Result is ADWellResult)
				{
					return Utility.GetDrawingADGenoTypeColor(((ADWellResult)well.Result).TypeNo);
				}
				break;
			case EColorMode.Target:
				return well.Project.Target.Items.Where((TargetItem s) => s.Name == item.TargetName).FirstOrDefault().Color;
			}
			return Colors.Black;
		}

		public ESeriesType GetChartDataLinePointStyle(SampleTargetItem item)
		{
			if (item.HasCustomLinePointStyle(_Experiment.ColorMode))
			{
				return item.GetCustomLinePointStyle(_Experiment.ColorMode);
			}
			return ESeriesType.None;
		}

		private void Root_SourceUpdated(object sender, DataTransferEventArgs e)
		{
			if (!(e.TargetObject is System.Windows.Controls.CheckBox) || ((System.Windows.Controls.CheckBox)e.TargetObject).IsChecked != false)
			{
				DumpInvoke.DataAnalyse(_Experiment, false);
				RefreshChartAxisX();
				RefreshChartAxisY();
				RefreshSeries();
			}
		}

		private void grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			RefreshSeries();
		}

		private void btnOK_Click(object sender, RoutedEventArgs e)
		{
			foreach (CurveParameterEx item in list)
			{
				string key = item.CurveParameter.WellNo.ToString("00") + item.CurveParameter.ChannelNo;
				if (!_Experiment.CurrentSubset.CurveParameters.Contains(key))
				{
					_Experiment.CurrentSubset.CurveParameters.Add(key, item.CurveParameter);
				}
			}
			base.DialogResult = true;
			Close();
		}

		private void btnCancel_Click(object sender, RoutedEventArgs e)
		{
			foreach (Subset.CurveParameter curveParameter in curveParameters)
			{
				string key = curveParameter.WellNo.ToString("00") + curveParameter.ChannelNo;
				_Experiment.CurrentSubset.CurveParameters[key] = curveParameter;
			}
			DumpInvoke.DataAnalyse(_Experiment, false);
			Close();
		}

		protected void RefreshChartAxisX()
		{
			double num = double.NaN;
			double num2 = double.NaN;
			List<SampleTargetItem> list = this.list.Select((CurveParameterEx s) => s.Item).ToList();
			foreach (SampleTargetItem item in list)
			{
				num = Utility.GetMin(num, item.Result.AMPCurve.GetCurveMinX());
				num2 = Utility.GetMax(num2, item.Result.AMPCurve.GetCurveMaxX());
			}
			if (!double.IsNaN(num) && !double.IsNaN(num2))
			{
				_Chart.MinX = num;
				_Chart.MaxX = num2;
			}
		}

		protected void RefreshChartAxisY()
		{
			double num = double.NaN;
			double num2 = double.NaN;
			List<SampleTargetItem> list = this.list.Select((CurveParameterEx s) => s.Item).ToList();
			foreach (SampleTargetItem item in list)
			{
				num = Utility.GetMin(num, item.Result.AMPCurve.GetCurveMinY());
				num2 = Utility.GetMax(num2, item.Result.AMPCurve.GetCurveMaxY());
			}
			if (!double.IsNaN(num) && !double.IsNaN(num2))
			{
				double num3 = (ConfigSettings.GetInstance().CurveSetting.MaxYTimes - 1.0) * (num2 - num);
				_Chart.MinY = num - num3;
				_Chart.MaxY = num2 + num3;
			}
		}

		private void grid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
		{
			if (e.Column.Header.ToString() != Utility.GetMessage("curve_parameter_use"))
			{
				CurveParameterEx curveParameterEx = e.Row.Item as CurveParameterEx;
				if (curveParameterEx != null && !curveParameterEx.CurveParameter.Use)
				{
					e.Cancel = true;
				}
			}
		}
	}
}
